package com.zipking.cloud.springbootmybatis.current;

import java.util.concurrent.ConcurrentSkipListMap;

public class StockPriceIndex {
    private final ConcurrentSkipListMap<String, Double> priceIndex = new ConcurrentSkipListMap<>();

    public void updatePrice(String stockSymbol, Double newPrice) {
        // 更新股票价格
        priceIndex.put(stockSymbol, newPrice);
    }

    public Double getPrice(String stockSymbol) {
        // 获取股票价格
        return priceIndex.get(stockSymbol);
    }

    public void removeStock(String stockSymbol) {
        // 移除股票信息
        priceIndex.remove(stockSymbol);
    }

    public ConcurrentSkipListMap<String, Double> headMap(String toKey) {
        // 获取指定范围内的股票价格索引
        return (ConcurrentSkipListMap<String, Double>) priceIndex.headMap(toKey);
    }

    public static void main(String[] args) {
        StockPriceIndex index = new StockPriceIndex();
        index.updatePrice("AAPL", 150.00);
        index.updatePrice("GOOGL", 2750.50);
        index.updatePrice("MSFT", 250.00);

        System.out.println("Price of AAPL: " + index.getPrice("AAPL"));
        System.out.println("Price of GOOGL: " + index.getPrice("GOOGL"));

        // 获取所有小于 "MSFT" 的股票价格索引
        ConcurrentSkipListMap<String, Double> subMap = index.headMap("MSFT");
        subMap.forEach((k, v) -> System.out.println("Stock: " + k + ", Price: " + v));
    }
}

